<?php

/**
 * DYSHOP
 * ============================================================================
 * 版权所有 2005-2028 杭州鼎易信息科技有限公司，并保留所有权利。
 * 网站地址: https://www.doing.net.cn
 * ==========================================
 * 本系统由鼎易科技团队开发 156791994@qq.com
 * ============================================================================
 * $Author: TANGYIN 2020-06-06 $
 *
 */
namespace app\api\controller;
use think\facade\View;
use think\facade\Db;
use think\facade\Config;
use think\facade\Request;
use app\BaseController;

class Base  extends BaseController
{
    public $page_size = 0;
    public $admin_id = 0;
    /**
     * 析构函数
     */
    function __construct(){
        header("Cache-control: private");

        Request::isAjax() ? define('IS_AJAX',true) : define('IS_AJAX',false);  //
        (Request::method() == 'GET') ? define('IS_GET',true) : define('IS_GET',false);  //
        (Request::method() == 'POST') ? define('IS_POST',true) : define('IS_POST',false);  //
        define('MODULE_NAME',app('http')->getName());  // 当前模块名称是
        define('CONTROLLER_NAME',Request::controller()); // 当前控制器名称
        define('ACTION_NAME',Request::action()); // 当前操作名称是

        define('PREFIX', config('database.connections.mysql.prefix')); // 数据库表前缀
        View::assign('action',ACTION_NAME);
        View::assign('template_now_time', time());//模板现在时间

        define('RUNTIME_PATH',root_path().'runtime' ); // 数据库表前缀
        define('APP_PATH', base_path());// 定义应用目录
        define('ROOT_PATH', root_path());// 定义应用目录

        //config设置
        $webconfig = DB::name("webconfig")->select();
        $webconfig_arr = [];
        foreach($webconfig as $v){
            $webconfig_arr[$v['varname']] = $v['varvalue'];
        }
        Config::set($webconfig_arr, 'webconfig');


        $this->checkLogin(); // 检查是否登录

        // 控制器初始化
        $this->initialize();
    }


    public function checkLogin(){
        // 判断哪些控制器的 哪些方法需要登录验证的 //暂时放置
        $check_arr = array(
            'users' => array('getUserInfo','saveUserInfo','pushHeadPic','user_center_update_mobile','user_center_order','system_detail','user_center_system','user_center_order_details','submitUserData','relieve_binding'
            ),
            'order'=>array('order_confirm','addOrder','order_confirm_form'),
            'weixinpay'=>array('wxPay'),
            'lists'=>array('addListsComment'),
        );
        //转换为小写
        $controller_name = strtolower(CONTROLLER_NAME);
        //$action_name = strtolower(ACTION_NAME);
        $action_name = ACTION_NAME;

        if(in_array($controller_name,array_keys($check_arr)) && in_array($action_name, $check_arr[$controller_name])){
//            if(!Cookie::get('user_id')){
//                $this->redirect(url('index/index'));
//            }
            if(input("param.token") == ''){
                return dyajaxReturn(1,'token不能为空');
            }


        }
    }

    public function ajaxReturn($data,$type = 'json'){
        exit(json_encode($data,JSON_UNESCAPED_UNICODE));
    }

    /**
     * 要验证的ajax
     * @param $act
     * @return bool
     */
    /*private function verifyAjaxRequest($act){
        $verifyAjaxArr = ['delGoodsCategory','delGoodsAttribute','delGoods','supertube_order'];
        if(request()->isAjax() && in_array($act,$verifyAjaxArr)){
            $res = $this->verifyAction();
            if($res['status'] == -1){
                $this->ajaxReturn($res);
            }else{
                return true;
            };
        }else{
            return true;
        }
    }*/




}